//
//  StoreDetaiViewController.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/4/27.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "StoreDetaiViewController.h"
#import "TraderManOfStoreCell.h"
#import "AddMiddleManViewController.h"

#import "AcitonBayStyleTitleView.h"
#import "ScroDeleteTableView.h"

#import "NodataCell.h"
#import "BrokerDM.h"

#import "BrokerOfStoreCell.h"
#import "StringTool.h"
#import "MJRefresh.h"

#import "EditBrokerVC.h"
#import "SelecteStoreVc.h"
#import "MBProgressHUD+MJ.h"

#import "FooterMenuView.h"
#import "AddMiddleManViewController2.h"
#import "SearchView.h"
#import "config.h"
#import "MBProgressHUD+MJ.h"
#import "HideTextVeiw.h"


@interface StoreDetaiViewController ()<UITableViewDelegate,UITableViewDataSource,BrokerOfStoreCellDelegate,FooterMenuViewDelegate,SearchViewDelegate,UITextViewDelegate,ScroDeleteTableViewDelgate>

@property (weak, nonatomic) IBOutlet UILabel *moenylab;
@property (weak, nonatomic) IBOutlet UITextField *seachField;

@property (weak, nonatomic) IBOutlet UILabel *middleManNumLab;

@property (nonatomic,strong)NSArray *dataArray;
@property (nonatomic,strong)AcitonBayStyleTitleView *titleView;
@property (nonatomic,weak)ScroDeleteTableView *mainTableView;
@property (weak, nonatomic) IBOutlet UIView *view1;

@property (nonatomic,strong)NodataCell *nodataCell;
@property (nonatomic,strong)BrokerDM *dataManage;
@property (nonatomic,assign)Boolean isEditMode;//是否点击了导航栏上边的 “编辑”按钮
@property (nonatomic,weak)FooterMenuView *footerView;
@property (nonatomic,assign)BOOL isClickSeleteAll; //是否点击了全选按钮

@property (nonatomic,strong)NSArray *seletedArr;//保存勾选的cell
@property (nonatomic,strong) SearchView *seachView;
@property (nonatomic,strong)NSArray *seachDataArray;
@property (nonatomic,copy)NSString *keyWord;
@property (nonatomic,assign)BOOL isSeachType;//是否搜索类型
@property (weak, nonatomic) IBOutlet UIButton *cacelBtn;

@property (nonatomic,assign)NSInteger pageIndex;//非搜索情况的页码
@property (nonatomic,assign)NSInteger searchIndex;//搜索情况下的页码
@property (weak, nonatomic) IBOutlet HideTextVeiw *searchTv;

@end

@implementation StoreDetaiViewController
@synthesize titleView,mainTableView,nodataCell,dataArray,dataManage,footerView,seachDataArray,keyWord,isSeachType,seachView;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    [self refreshData];

}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.searchTv resignFirstResponder ];
}
-(void)initView
{
    [self.searchTv setHideText:@"可通过经纪人的姓名搜索"];
    dataManage = [[BrokerDM alloc]init];
    
    self.cacelBtn.enabled = NO;
    [self.cacelBtn setTitle:@"" forState:UIControlStateNormal];
    
    if (![StringTool isBlankString:self.storeModel.brokerages]) {
        self.moenylab.text = self.storeModel.brokerages;
    }else
    {
        self.moenylab.text = @"0";
    }
    
    self.middleManNumLab.text = [NSString stringWithFormat:@"%zd",self.storeModel.brokeCounts];
    
    titleView = [AcitonBayStyleTitleView creadView];
    [[titleView abackBtn]addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [[titleView aaddBtn]addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [[titleView aeditBtn]addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [[titleView atitleLab]setText:self.storeModel.storeName];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:titleView];
    
    
    [self.seachField setLeftViewMode:UITextFieldViewModeAlways];
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 38, 44)];
    
    UIImageView *leftView = [[UIImageView alloc]initWithFrame:CGRectMake(backView.frame.size.width/2-8, backView.frame.size.height/2-8, 15, 15)];
    [leftView setImage:[UIImage imageNamed:@"search_icom"]];
    [backView addSubview:leftView];
    [self.seachField setLeftView:backView];
    
    
   
    CGFloat tableViewY = self.view1.frame.size.height + self.view1.frame.origin.y;
    ScroDeleteTableView *tableView = [[ScroDeleteTableView alloc]initWithFrame:CGRectMake(0, tableViewY, UIScreenWidth, UIScreenHeight - tableViewY-20)];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.menuStatedelegate = self;
    [self.view addSubview:tableView];
    mainTableView = tableView;
    
    //下拉刷新
    [mainTableView addLegendHeaderWithRefreshingBlock:^{
        [self refreshData];
        
    }];
    //上拉加载
    [mainTableView addLegendFooterWithRefreshingBlock:^{
        [self loadMore];
    }];
    
    
    //添加footerview
    FooterMenuView *menuView = [FooterMenuView creatView];
    menuView.delegate = self;
    menuView.hidden = YES;
    [self.view addSubview:menuView];
    self.footerView = menuView;
    
    
    seachView = [[SearchView alloc]initWithParentView:self.mainTableView withEt:self.seachField];
    seachView.delegate = self;
    [seachView.tableView  addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(searchLoadMore)];
    seachView.textView = self.searchTv;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (dataArray.count == 0 || dataArray == nil) {
         [tableView setScrollEnabled:false];
        return 1;
    }else
    {
         [tableView setScrollEnabled:true];
        return dataArray.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (dataArray.count == 0 || dataArray == nil) {
        if (!nodataCell) {
            nodataCell = [[NodataCell alloc]initWithFrame:mainTableView.frame];
            [nodataCell setState:nodataCellStateLoading];
            [nodataCell.refreshBtn setTag:56];
            [nodataCell.refreshBtn addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        }
        return nodataCell;
    }
    
    static NSString *ID = @"brokerOfStoreCell";
    BrokerOfStoreCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil)
    {
        cell = [[BrokerOfStoreCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        [cell.contentMoveView setTag:indexPath.row];
        [cell.contentMoveView addTarget:self action:@selector(cellOnClick:) forControlEvents:UIControlEventTouchUpInside];
        cell.btnDelegate = self;
        [cell initWithTableView:self.mainTableView];
    }

    
    
    BorkerCellModel *mod = [dataArray objectAtIndex:indexPath.row];
    
    if (mod.isMoved) {
        
        [cell setIsEditMode:false];
        [cell.brokerOfStoreView hideRightView:true];
        [cell setUserInteractionEnabled:false];
        
        [cell.brokerOfStoreView setBackgroundColor:UIColorFromRGB(0xf3f3f3)];
        
    }else{
        
        [cell.brokerOfStoreView setBackgroundColor:[UIColor clearColor]];
        [cell setIsEditMode:self.isEditMode];
        [cell.brokerOfStoreView hideRightView:false];
        [cell setUserInteractionEnabled:true];
    }
    
    cell.model = mod;
    
    return cell;

}

-(void)cellOnClick:(UIButton *)btn
{
    
    BorkerCellModel  *data = [dataArray objectAtIndex:btn.tag];
    
    [data setIsSelected:!data.isSelected];
    [self.mainTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:btn.tag inSection:0]] withRowAnimation:UITableViewRowAnimationFade];

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (dataArray.count == 0 || dataArray == nil) {
        return mainTableView.frame.size.height;
    }
    return [BrokerOfStoreCell getHeight];
}


#pragma mark 点击btn
-(void)btnOnclick:(UIButton *)btn
{
    if (btn.tag == 56) {
        [self refreshData];
    }else if (btn == titleView.abackBtn)//返回
    {
        [self.navigationController popViewControllerAnimated:YES];
    }else if (btn == titleView.aeditBtn)//编辑
    {
       
        if (self.dataArray.count == 0 || self.dataArray == nil)
            return;
        
        self.isEditMode = !self.isEditMode;
         footerView.hidden =  !self.isEditMode;
        [self.mainTableView reloadData];
        
        
    }else if (btn == titleView.aaddBtn)//添加
    {
        AddMiddleManViewController2 *vc = [[AddMiddleManViewController2 alloc]initWithBlock:^(int state) {
            
            if (state == 1) {
                
                [self refreshData];
                
                if (![StringTool isBlankString:self.storeModel.brokerages]) {
                    self.moenylab.text = self.storeModel.brokerages;
                }else
                {
                    self.moenylab.text = @"0";
                }
                NSInteger count = self.storeModel.brokeCounts + 1;
                self.middleManNumLab.text = [NSString stringWithFormat:@"%zd",count];

            }
            
        }];
        vc.storeModel = self.storeModel;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark  获取数据
-(void)refreshData
{
       self.pageIndex = 1;
       [mainTableView.footer resetNoMoreData];
        [mainTableView reloadData];
        [nodataCell setState:nodataCellStateLoading];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod = [dataManage refreshDataWithStoreCode:self.storeModel.storeCode withKeyWord:@"*"];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (mod.resltState == ResultFail) {
                [nodataCell setCodeErrorTip:mod.resultCode];
            }else if (mod.resltState == ResultNetError)
            {
                [nodataCell setState:nodataCellStateNetError];
            }else
            {
                dataArray = [[NSArray alloc]initWithArray:mod.dataMod];
                if (dataArray.count == 0 || dataArray == nil) {
                    [nodataCell setState:nodataCellStateNodata];
                }
            }
            [mainTableView.header endRefreshing];
            [mainTableView reloadData];
            self.pageIndex = (dataArray.count == 0||dataArray == nil)?1:2;
            
        });
    });
}

#pragma mark 搜索的时候获取数据
-(void)searchrefeshData
{
    [seachView.tableView.footer resetNoMoreData];
    
    __weak typeof(self)bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod = [dataManage refreshDataWithStoreCode:self.storeModel.storeCode withKeyWord:self.keyWord];
        dispatch_async(dispatch_get_main_queue(), ^{

            if (mod.resltState == ResultFail) {
                
                [MBProgressHUD showError:mod.resultTip];
                
            }else if (mod.resltState == ResultNetError)
            {
                [MBProgressHUD showError:@"网络错误"];
            }else if (mod.resltState == ResultSuccess){
                
                seachDataArray = [[NSArray alloc]initWithArray:mod.dataMod];
                [seachView.tableView reloadData];
                [seachView tableViewReloadAndUpFrame];
                
                bself.searchIndex = (seachDataArray.count == 0 || seachDataArray == nil)?1:2;
            
            }
        });
    });

}

#pragma mark 搜索情况下的加载更多
-(void)searchLoadMore
{
    [seachView.tableView.footer resetNoMoreData];
    
    __weak typeof(self)bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        ResulteMod *mod  = [dataManage loadMoreDataWithKeyword:self.keyWord withIndex:bself.searchIndex];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [bself.seachView.tableView.footer endRefreshing];
            if (mod.resltState == ResultFail) {
                
                [MBProgressHUD showError:mod.resultTip];
                
            }else if (mod.resltState == ResultNetError)
            {
                 [MBProgressHUD showError:@"网络错误"];;
            }else
            {
                    NSMutableArray *arrM = [NSMutableArray arrayWithArray:mod.dataMod];
                    if (arrM.count > 0) {
                    bself.searchIndex ++;
                    }else{
                        [bself.seachView.tableView.footer noticeNoMoreData];
                    }

                    [arrM addObjectsFromArray:bself.seachDataArray];
                    bself.seachDataArray = [[NSArray alloc]initWithArray:arrM];
                    [bself.seachView.tableView reloadData];
                    [seachView tableViewReloadAndUpFrame];
                
                
            }
        });
    });

}
#pragma mark 非搜索情况下加载更多
-(void)loadMore
{
    [mainTableView reloadData];
    [nodataCell setState:nodataCellStateLoading];
    [mainTableView.footer resetNoMoreData];
    
    __weak typeof(self)bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod  = [dataManage loadMoreDataWithKeyword:@"*" withIndex:bself.pageIndex];

        dispatch_async(dispatch_get_main_queue(), ^{
           
            [mainTableView.footer endRefreshing];
            if (mod.resltState == ResultFail) {
                [nodataCell setCodeErrorTip:mod.resultCode];
            }else if (mod.resltState == ResultNetError)
            {
                [nodataCell setState:nodataCellStateNetError];
            }else
            {
                NSMutableArray *arrM = [NSMutableArray arrayWithArray:mod.dataMod];
                    if (arrM.count == 0 || arrM == nil) {
                        [mainTableView.footer noticeNoMoreData];
                    }
                    [arrM addObjectsFromArray:dataArray];
                    dataArray = [[NSArray alloc]initWithArray:arrM];
                if (arrM.count > 0) {
                    bself.pageIndex ++;
                }
                   
            }
                [bself.mainTableView reloadData];
            
            
        });
    });

}

#pragma mark brokerofstorecell的代理，点击了“编辑，移动，启用）
-(void)brokerOfStoreCellFunctionBtnOnClick:(UIButton *)btn WithModel:(BorkerCellModel *)model
{
    switch (btn.tag) {
        case 101: //编辑
        {
            EditBrokerVC *vc = [EditBrokerVC new];
            vc.model = model;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 102://禁用，启用
        {
            [self modifYuserstatus:model];
        }
            
            break;
        case 103://移动
        {
            __weak typeof(self)bself = self;
            
            SelecteStoreVc *vc = [[SelecteStoreVc alloc]initWithBlock:^(NearbyCellData *data) {
                
                [bself refreshData];
                
                if (![StringTool isBlankString:bself.storeModel.brokerages]) {
                    bself.moenylab.text = bself.storeModel.brokerages;
                }else
                {
                    bself.moenylab.text = @"0";
                }
                
                NSInteger count = bself.storeModel.brokeCounts - 1;
                bself.middleManNumLab.text = [NSString stringWithFormat:@"%zd",count];

            }];
            
            vc.selectedFlag = model.storeName;
            vc.model = model;
            vc.vcType = BrokerManageVcType;
            [self.navigationController pushViewController:vc animated:YES];
        }
            
            break;
            
        default:
            break;
    }
}

#pragma mark 启用，禁用操作（0进行启用操作，1进行禁用操作）
-(void)modifYuserstatus:(BorkerCellModel *)model
{
    [MBProgressHUD showMessage:@"请稍等" toView:self.view];
    
    __weak typeof(self)bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod = [dataManage modifyuserstatusWithModel:model];
        
        sleep(1);
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [MBProgressHUD hideHUDForView:bself.view animated:YES];
            
            if (mod.resltState == ResultFail) {
                [MBProgressHUD showError:mod.resultTip];
            }else if (mod.resltState == ResultNetError)
            {
                [MBProgressHUD showError:@"网络错误"];
            }else
            {
                [MBProgressHUD showSuccess:@"操作成功"];
                
                [bself refreshData];
            }
            
            
        });
    });
    
}


#pragma mark footerview的代理  全选按钮被点击
-(void)footerMenuViewSelectAllBtnOnClicked;
{
    self.isClickSeleteAll = !self.isClickSeleteAll;
    [footerView.selectAllBtn setHighlighted:self.isEditMode];
    
    for (BorkerCellModel *model in dataArray) {
        model.isSelected = self.isClickSeleteAll;
    }
    [self.mainTableView reloadData];
}
#pragma mark footerview的代理 启用按钮
-(void)footerMenuViewBeAbleBtnOnClicked;
{
    if ([[self getAllborkerIds] isEqualToString:@"NO"]) {
        [MBProgressHUD showError:@"请至少选择一个客户"];
        return;
    }
    
    [self clickOnAbleOrUnAbleBtnUpdataWithborkerIds:[self getAllborkerIds] WithStatus:@"0"];
}
#pragma mark footerview的代理 禁用按钮
-(void)footerMenuViewbeUnAbleBtnOnClicked;
{
    if ([[self getAllborkerIds] isEqualToString:@"NO"]) {
        [MBProgressHUD showError:@"请至少选择一个客户"];
        return;
    }
    [self clickOnAbleOrUnAbleBtnUpdataWithborkerIds:[self getAllborkerIds] WithStatus:@"1"];
    
   
}
#pragma mark footerview的代理 移动按钮(批量移动）
-(void)footerMenuViewbeMoveBtnOnClicked;
{
    if ([[self getAllborkerIds] isEqualToString:@"NO"]) {
        [MBProgressHUD showError:@"请至少选择一个客户"];
        return;
    }

    __weak typeof(self)bself = self;
    
    SelecteStoreVc *vc = [[SelecteStoreVc alloc]initWithBlock:^(NearbyCellData *data) {
        
        [bself refreshData];
        
        if (![StringTool isBlankString:bself.storeModel.brokerages]) {
            bself.moenylab.text = bself.storeModel.brokerages;
        }else
        {
            bself.moenylab.text = @"0";
        }
        
        NSInteger count = bself.storeModel.brokeCounts - bself.seletedArr.count;
        bself.middleManNumLab.text = [NSString stringWithFormat:@"%zd",count];
        bself.footerView.selectAllBtn.highlighted = NO;

    }];
    
    vc.selectedFlag = self.storeModel.storeName;
    vc.borkerIds = [self getAllborkerIds];
    vc.storeCode = self.storeModel.storeCode;
    vc.vcType = BathMoveStore;
    [self.navigationController pushViewController:vc animated:YES];
    
   
}

#pragma mark 点击了“禁用或启用后提交数据(批量的）
-(void)clickOnAbleOrUnAbleBtnUpdataWithborkerIds:(NSString *)borkerIds WithStatus:(NSString *)status;
{
    [MBProgressHUD showMessage:@"请稍等" toView:self.view];
    
    __weak typeof(self)bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod = [dataManage batchmodifyuserstatusWithBorkerId:borkerIds withStatuts:status];
        dispatch_async(dispatch_get_main_queue(), ^{
             [footerView.selectAllBtn setHighlighted:NO];
            [MBProgressHUD hideHUDForView:bself.view animated:YES];
            
            
             footerView.selectAllBtn.highlighted = NO;
            if (mod.resltState == ResultFail) {
                
                [MBProgressHUD showError:mod.resultTip];
            }else if (mod.resltState == ResultNetError)
            {
                [MBProgressHUD showError:@"网络错误"];
            }
            else
            {
                [MBProgressHUD showSuccess:@"操作成功"];
                [bself refreshData];
            }
            
        });
    });
}

#pragma mark 获取被勾选经纪人ID
-(NSString *)getAllborkerIds
{
    NSMutableArray *arrM = [NSMutableArray array];
    
    for (BorkerCellModel *model in dataArray) {
        if (model.isSelected) {
            
            [arrM addObject:model.brokerId];
        }
    }
    
    if (arrM.count == 0) {
        return @"NO";
    }
    self.seletedArr = [[NSArray alloc]initWithArray:arrM];
    return  [arrM componentsJoinedByString:@","];
}


#pragma mark seachView delegate
-(NSArray*)getDataByKewWord:(NSString*)kewWord{
    
    return seachDataArray;
}


-(CGFloat)getCellHight{
    
    return 60;
}

-(NSInteger)getCellCount{
    
    return seachDataArray.count;
}

-(UITableViewCell*)getCellByTableView:(UITableView *)tableView WithIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ID = @"brokerOfStoreCell";
    BrokerOfStoreCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil)
    {
        cell = [[BrokerOfStoreCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        [cell.contentMoveView setTag:indexPath.row];
        [cell.contentMoveView addTarget:self action:@selector(cellOnClick:) forControlEvents:UIControlEventTouchUpInside];
        cell.btnDelegate = self;
        [cell initWithTableView:self.mainTableView];
    }
    
     BorkerCellModel *mod = [seachDataArray objectAtIndex:indexPath.row];
    
    if (mod.isMoved) {
        
        [cell setIsEditMode:false];
        [cell.brokerOfStoreView hideRightView:true];
        [cell setUserInteractionEnabled:false];
        
    }else{
        
        [cell setIsEditMode:self.isEditMode];
        [cell.brokerOfStoreView hideRightView:false];
        [cell setUserInteractionEnabled:true];
    }
    
    cell.model = mod;
    
    return cell;
}

-(void)hideSearchView
{
    isSeachType = NO;
    seachDataArray = nil;
    self.seachField.text = @"";
    
    self.cacelBtn.enabled = NO;
    [self.cacelBtn setTitle:@"" forState:UIControlStateNormal];
}

#pragma mark textField delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    isSeachType = YES;
    self.cacelBtn.enabled = YES;
    [self.cacelBtn setTitle:@"取消" forState:UIControlStateNormal];
    
    [self.seachView show];
    
    [self.seachView tableViewReloadAndUpFrame];
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{
    self.seachField.text = @"";
    isSeachType = NO;
    seachDataArray = nil;
    [self.seachView hide];
    [self.seachField resignFirstResponder];
    self.cacelBtn.enabled = NO;
    [self.cacelBtn setTitle:@"" forState:UIControlStateNormal];
    
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    
    if (!isSeachType) {
        return YES;
    }
    NSMutableString *keyStr = [NSMutableString stringWithString:textField.text];
    
    if(![StringTool isBlankString:string])
    {
        [keyStr appendString:string];
    }else
    {
        NSUInteger length = keyStr.length - 1;
        
        NSRange rang =  NSMakeRange(0, length);
        
        keyStr =  [NSMutableString stringWithString:[keyStr substringWithRange:rang]];
    }
    
    keyWord = keyStr;
    
    keyWord = keyStr.length >0? keyStr:@"???????";
    if(keyWord.length >= 2)
    {
        [self searchrefeshData];
        
        [self.seachView tableViewReloadAndUpFrame];
    }
    
    return true;
}
- (IBAction)clickCancelBtn:(id)sender {
    
    [self.searchTv setText:@""];
    self.keyWord = @"**********";
    
    [self.searchTv resignFirstResponder];
    [self.searchTv textDicChange:@""];;
    [self.cacelBtn setTitle:@"" forState:UIControlStateNormal];
    self.cacelBtn.enabled = NO;
    self.seachDataArray = nil;
    
    
    [self.seachView hide];

}


- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
{
    if (dataArray.count == 0 ||dataArray == nil) {
        return YES;
    }
    
    [self.searchTv textDicChange:@""];
    
    [self.cacelBtn setTitle:@"取消" forState:UIControlStateNormal];
    self.cacelBtn.enabled = YES;

    [self.seachView show];
    
    [self.seachView tableViewReloadAndUpFrame];
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView;
{
    [self.searchTv textDicChange:textView.text];
    //处理空格
    NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSString *trimedString = [textView.text stringByTrimmingCharactersInSet:set];
    
    self.keyWord = textView.text;
    if (trimedString.length == 0) {
        self.keyWord = @"***********";
        
    }
    [self searchrefeshData];
    [self.seachView tableViewReloadAndUpFrame];}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        [self.searchTv setText:@""];;
        [self.searchTv textDicChange:@""];
        [self.seachView hide];
        self.seachDataArray = nil;
        
        [self.cacelBtn setTitle:@"" forState:UIControlStateNormal];
        self.cacelBtn.enabled = NO;

        ;
    }
    self.keyWord = @"***********";
    return YES;
}

#pragma mark menuScroTableView delegate

-(void)cellMenDidHide:(UITableView*)tableView  cell:(ScroDeleteCell*)cell{
    
    BrokerOfStoreCell *brokerCell = (BrokerOfStoreCell *)cell;
    [brokerCell.brokerOfStoreView.rightBtn setImage:[UIImage imageNamed:@"right_back_shift"] forState:UIControlStateNormal];
}


-(void)cellMenDidShow:(UITableView*)tableView cell:(ScroDeleteCell*)cell{
    BrokerOfStoreCell *brokerCell = (BrokerOfStoreCell *)cell;
    [brokerCell.brokerOfStoreView.rightBtn setImage:[UIImage imageNamed:@"right_jiantou2"] forState:UIControlStateNormal];
    
}


@end
